<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'
import { number } from 'echarts/lib/export'
import { listHistory } from '@/api/iot/deviceLog';
export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '240px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            // required: true,
        },
        num: {
            type: String,
            default: 'today'
        },
        isNumber: {
            type: String | number,
        },
        valueTimes: {
            type: Array,
            default: []
        }
    },
    data() {
        return {
            chart: null,
            isListData: [],
            timeData: [],
        }
    },
    watch: {
        num: {
            deep: true,
            handler(val) {
                this.listHistoryFun()
            }
        },
        isNumberData: {
            deep: true,
            handler(val) {
                this.listHistoryFun()
            }
        },
        valueTimes: {
            deep: true,
            handler(val) {
                if (val) {
                    this.timeData = val
                } else {
                    this.timeData = []
                }
                this.listHistoryFun()
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        formatDate(date) {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            const hours = String(date.getHours()).padStart(2, '0');
            const minutes = String(date.getMinutes()).padStart(2, '0');
            return `${year}-${month}-${day} ${hours}:${minutes}`;
        },

        listHistoryFun() {
            let beginTime = '';
            let endTime = '';
            const now = new Date();
            // if (this.num === 'today') {
            //     const start = new Date(now.getFullYear(), now.getMonth(), now.getDate());
            //     const end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
            //     beginTime = this.formatDate(start);
            //     endTime = this.formatDate(end);
            // } else if (this.num === 'yesterday') {
            //     const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
            //     const end = new Date(now.getFullYear(), now.getMonth(), now.getDate());
            //     beginTime = this.formatDate(start);
            //     endTime = this.formatDate(end);
            // } else if (this.num === 'week') {
            //     const firstDay = new Date(now.setDate(now.getDate() - now.getDay() + 1));
            //     const lastDay = new Date(now.setDate(now.getDate() - now.getDay() + 7));
            //     beginTime = this.formatDate(firstDay);
            //     endTime = this.formatDate(lastDay);
            // } else if (this.num === 'month') {
            //     const start = new Date(now.getFullYear(), now.getMonth(), 1);
            //     const end = new Date(now.getFullYear(), now.getMonth() + 1, 0);
            //     beginTime = this.formatDate(start);
            //     endTime = this.formatDate(end);
            // }
            if (this.timeData.length > 0) {
                beginTime = this.timeData[0]
                endTime = this.timeData[1]
            } else {
                const start = new Date(now.getFullYear(), now.getMonth(), now.getDate());
                const end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
                beginTime = this.formatDate(start);
                endTime = this.formatDate(end);
            }
            if (!this.isNumber) {
                return
            }
            listHistory({
                serialNumber: this.isNumber,
                // serialNumber: 'D13NT1TM0369',
                beginTime: beginTime,
                endTime: endTime
            }).then((res) => {
                let arr = [];
                arr = Object.keys(res.data)
                // 判断 arr 数组是否包含 VD180
                const hasVD180 = arr.includes('VD180');
                if (hasVD180) {
                    this.isListData = res.data['VD180']
                } else {
                    this.isListData = res.data[arr[0]]
                }

                this.setOptions()
            })
        },
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // this.setOptions(this.chartData)
            this.listHistoryFun()
        },
        setOptions() {
            this.chart.setOption({
                animationDurationUpdate: 3000,
                xAxis: {
                    type: 'time',
                    axisLabel: {
                        color: '#409EFF' // x 轴标签文字颜色
                    },
                },
                grid: {
                    left: 10,
                    right: 10,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    type: 'value',
                    scale: true,//自适应
                    axisLabel: { formatter: function (value, index) { return value.toFixed(2); } }//可根据实际情况修改y轴保留小数位数
                },
                series: [
                    {
                        name: '出水量',
                        type: 'line',
                        symbol: 'none',
                        sampling: 'lttb',
                        itemStyle: {
                            color: '#409EFF',
                        },
                        areaStyle: {},
                        data: this.isListData.map(item => [item.time, item.value]),
                        // animationDuration: 2800,
                        // animationEasing: 'quadraticOut'
                    }]
            })
        }
    }
}
</script>
